use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(Products::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(Products::Id)
                            .string()
                            .not_null()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(Products::BrandId).string().not_null())
                    .col(ColumnDef::new(Products::ProductImageId).string().not_null())
                    .col(ColumnDef::new(Products::ProductVideoId).string().not_null())
                    .col(ColumnDef::new(Products::ProductSkuId).string().not_null())
                    .col(ColumnDef::new(Products::TaxClassId).string().not_null())
                    .col(ColumnDef::new(Products::Slug).string())
                    .col(ColumnDef::new(Products::Variables).json())
                    .col(
                        ColumnDef::new(Products::IsVirtual)
                            .tiny_integer()
                            .not_null(),
                    )
                    .col(ColumnDef::new(Products::Position).integer().not_null())
                    .col(
                        ColumnDef::new(Products::Active)
                            .boolean()
                            .not_null()
                            .default(true),
                    )
                    .col(ColumnDef::new(Products::Weight).decimal().not_null())
                    .col(ColumnDef::new(Products::WeightClass).string().not_null())
                    .col(ColumnDef::new(Products::Sales).integer().not_null())
                    .col(ColumnDef::new(Products::Viewed).integer().not_null())
                    .col(
                        ColumnDef::new(Products::PublishedAt)
                            .timestamp_with_time_zone()
                            .null(),
                    )
                    .col(
                        ColumnDef::new(Products::DeletedAt)
                            .timestamp_with_time_zone()
                            .null(),
                    )
                    .col(
                        ColumnDef::new(Products::CreatedAt)
                            .timestamp_with_time_zone()
                            .default(Expr::current_timestamp()),
                    )
                    .col(
                        ColumnDef::new(Products::UpdatedAt)
                            .timestamp_with_time_zone()
                            .default(Expr::current_timestamp()),
                    )
                    .to_owned(),
            )
            .await
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(Products::Table).to_owned())
            .await
    }
}

#[derive(DeriveIden)]
enum Products {
    Table,
    Id,
    BrandId,
    ProductImageId,
    ProductVideoId,
    ProductSkuId,
    TaxClassId,
    Slug,
    Variables,
    IsVirtual,
    Position,
    Active,
    Weight,
    WeightClass,
    Sales,
    Viewed,
    PublishedAt,
    DeletedAt,
    CreatedAt,
    UpdatedAt,
}
